package com.meteor.downloader.videodownloader.activity

import android.content.Intent
import android.view.View
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import com.hjq.language.MultiLanguages
import com.meteor.downloader.videodownloader.R
import com.meteor.downloader.videodownloader.adapter.AdapterLanguageSetItem
import com.meteor.downloader.videodownloader.admdm.MdmDealAdUtils
import com.meteor.downloader.videodownloader.databinding.ActivityLanguageInitMdmBinding
import com.meteor.downloader.videodownloader.dismissGoMdm
import com.meteor.downloader.videodownloader.mdmdata.DataMdmLanguage
import com.meteor.downloader.videodownloader.utils.MdmTotalUtils
import com.meteor.downloader.videodownloader.utils.dialog.DialogInitMdmLoading
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import java.util.Locale

class MdmLanguageInitActivity : MdmBindingActivity<ActivityLanguageInitMdmBinding>() {
    private lateinit var mAdapterLanguageSetItem: AdapterLanguageSetItem
    private val mListDoData = mutableListOf<DataMdmLanguage>()
    private var mMdmSelectIndex = -1
    override fun doBackPressed() {
        showBookAdMdm("mdmBack") {
            super.doBackPressed()
        }
    }

    override fun beginViewAndDoMdm() {
        mStarNativeValue = true
        mNativeBookLayout = mMdmBinding.flMdmNativeView
        mMdmBinding.layoutTopSet.ivBackAllMdm.visibility = View.INVISIBLE
        mMdmBinding.layoutTopSet.tvTitleAll.text = getString(R.string.mdm_language)
        setMdmRecyclerView()
        mMdmBinding.layoutTopSet.ivSet.setOnClickListener {
            showBookAdMdm("", true) {
                for (data in mListDoData) {
                    if (data.fingerSelect) {
                        MultiLanguages.setAppLanguage(this, data.mdmLocale)
                        startActivity(Intent(this, MainMdmActivity::class.java).apply {
                        })
                        finish()
                        break
                    }
                }
            }
        }
        lifecycleScope.launch(Dispatchers.Main) {
            delay(124)
            MdmDealAdUtils.loadBookIntersMdm()
        }
    }

    override fun setMdmRecyclerView() {
        initLanguageDataMdm()
        mAdapterLanguageSetItem = AdapterLanguageSetItem(this, mListDoData) {
            val data = mListDoData[it]
            if (data.fingerSelect) return@AdapterLanguageSetItem
            for (i in mListDoData.indices) {
                if (mListDoData[i].fingerSelect) {
                    mListDoData[i].fingerSelect = false
                    mAdapterLanguageSetItem.notifyItemChanged(i)
                    break
                }
            }
            data.fingerSelect = true
            mAdapterLanguageSetItem.notifyItemChanged(it)
        }
        mMdmBinding.rvMdmList.layoutManager = LinearLayoutManager(this)
        mMdmBinding.rvMdmList.adapter = mAdapterLanguageSetItem
        mMdmBinding.rvMdmList.postDelayed({
            mMdmBinding.rvMdmList.smoothScrollToPosition(mMdmSelectIndex + 3)
        }, 800)
    }

    private fun initLanguageDataMdm() {
        mListDoData.add(DataMdmLanguage(getString(R.string.mdm_en), Locale("en", "US")))
        mListDoData.add(DataMdmLanguage(getString(R.string.mdm_es), Locale("es", "US")))
        mListDoData.add(DataMdmLanguage(getString(R.string.mdm_pt), Locale("pt", "BR")))
        mListDoData.add(DataMdmLanguage(getString(R.string.mdm_fr), Locale("fr", "FR")))
        mListDoData.add(DataMdmLanguage(getString(R.string.mdm_in), Locale("in", "ID")))
        mListDoData.add(DataMdmLanguage(getString(R.string.mdm_tr), Locale("tr", "TR")))
        mListDoData.add(DataMdmLanguage(getString(R.string.mdm_ar), Locale("ar", "AE")))
        mListDoData.add(DataMdmLanguage(getString(R.string.mdm_de), Locale("de", "DE")))
        mListDoData.add(DataMdmLanguage(getString(R.string.mdm_th), Locale("th", "TH")))
        mListDoData.add(DataMdmLanguage(getString(R.string.mdm_it), Locale("it", "IT")))
        mListDoData.add(DataMdmLanguage(getString(R.string.mdm_vi), Locale("vi", "VN")))
        mListDoData.add(DataMdmLanguage(getString(R.string.mdm_hi), Locale("hi", "IN")))
        mListDoData.add(DataMdmLanguage(getString(R.string.mdm_fil), Locale("fil", "PH")))
        mListDoData.add(DataMdmLanguage(getString(R.string.mdm_ko), Locale("ko", "KR")))
        mListDoData.add(DataMdmLanguage(getString(R.string.mdm_ja), Locale("ja", "JP")))
        mListDoData.add(DataMdmLanguage(getString(R.string.mdm_bn), Locale("bn", "BD")))

        val locale = MultiLanguages.getAppLanguage(this)
        for (index in mListDoData.indices) {
            val data = mListDoData[index]
            if (data.mdmLocale.language == locale.language) {
                data.fingerSelect = true
                mMdmSelectIndex = index
                break
            }
        }
        if (mMdmSelectIndex == -1) {
            mMdmSelectIndex = 0
            mListDoData[0].fingerSelect = true
        }
    }

    private fun showBookAdMdm(
        type: String, showTip: Boolean = false, statusLook: Boolean = true, mdmBack: () -> Unit
    ) {
        if (statusLook && !MdmTotalUtils.getMdmAppStatus()) {
            mdmBack()
            return
        }
        if (showTip) {
            if (MdmDealAdUtils.getVlaueForInters() != null) {
                showRootBookAdBack(type, statusLook, mdmBack)
                return
            }
            val dialog = DialogInitMdmLoading(this)
            dialog.show()
            MdmDealAdUtils.loadBookIntersMdm(true, statusLook)
            lifecycleScope.launch(Dispatchers.Main) {
                delay(1600)
                if (MdmDealAdUtils.getVlaueForInters() != null) {
                    showRootBookAdBack(type, statusLook, mdmBack)
                    delay(96)
                    dialog.dismissGoMdm()
                    return@launch
                }
                if (MdmDealAdUtils.getVlaueForInters() != null) {
                    delay(1600)
                    showRootBookAdBack(type, statusLook, mdmBack)
                    delay(100)
                    dialog.dismissGoMdm()
                    return@launch
                }
                delay(1600)
                showRootBookAdBack(type, statusLook, mdmBack)
                delay(96)
                dialog.dismissGoMdm()
            }
        } else {
            showRootBookAdBack(type, statusLook, mdmBack)
        }
    }

    override fun getLayoutValue(): ActivityLanguageInitMdmBinding {
        return ActivityLanguageInitMdmBinding.inflate(layoutInflater)
    }
}